package com.zcoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcoj.core.model.PageData;
import com.zcoj.core.model.PageWrap;
import com.zcoj.mapper.GroupProblemMapper;
import com.zcoj.pojo.Dto.GroupProblem;
import com.zcoj.service.GroupProblemService;
import com.zcoj.util.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/4/21 21:08
 */
@Service
public class GroupProblemServiceImpl implements GroupProblemService {


 @Autowired
 private GroupProblemMapper groupProblemMapper;

 @Override
 public Integer create(GroupProblem groupProblem) {
  groupProblemMapper.insert(groupProblem);
  return groupProblem.getId();
 }

 @Override
 public void deleteById(Integer id) {
  groupProblemMapper.deleteById(id);
 }

 @Override
 public void deleteByIdInBatch(List<Integer> ids) {
  if (CollectionUtils.isEmpty(ids)) return;
  groupProblemMapper.deleteBatchIds(ids);
 }

 @Override
 public void updateById(GroupProblem groupProblem) {
  groupProblemMapper.updateById(groupProblem);
 }

 @Override
 public void updateByIdInBatch(List<GroupProblem> groupProblems) {
  if (CollectionUtils.isEmpty(groupProblems)) return;
  for (GroupProblem groupProblem: groupProblems) {
   this.updateById(groupProblem);
  }
 }

 @Override
 public GroupProblem findById(Integer id) {
  return groupProblemMapper.selectById(id);
 }

 @Override
 public GroupProblem findOne(GroupProblem groupProblem) {
  Wrapper<GroupProblem> wrapper = new QueryWrapper<>(groupProblem);
  return groupProblemMapper.selectOne(wrapper);
 }

 @Override
 public List<GroupProblem> findList(GroupProblem groupProblem) {
  Wrapper<GroupProblem> wrapper = new QueryWrapper<>(groupProblem);
  return groupProblemMapper.selectList(wrapper);
 }

 @Override
 public PageData<GroupProblem> findPage(PageWrap<GroupProblem> pageWrap) {
  IPage<GroupProblem> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
  QueryWrapper<GroupProblem> queryWrapper = new QueryWrapper<>(WrapperUtil.blankToNull(pageWrap.getModel()));
  for(PageWrap.SortData sortData: pageWrap.getSorts()) {
   if (sortData.getDirection().equalsIgnoreCase("DESC")) {
    queryWrapper.orderByDesc(sortData.getProperty());
   } else {
    queryWrapper.orderByAsc(sortData.getProperty());
   }
  }
  return PageData.from(groupProblemMapper.selectPage(page, queryWrapper));
 }

 @Override
 public long count(GroupProblem groupProblem) {
  Wrapper<GroupProblem> wrapper = new QueryWrapper<>(groupProblem);
  return groupProblemMapper.selectCount(wrapper);
 }
}
